<?php
class recommend_c extends Control
{
	var $subject;
	function __construct()
	{
		parent::Control();
		$this->load_model("user");
		$this->load_model("recommend");
	}

	function recommend_c()
	{
		$this->__construct();
	}

	function index_f()
	{
		if(!$_SESSION["user_id"])
		{
			error('',$this->url('login')."&rf=recommend");
		}
		$rs = $this->user_m->user_from_id($_SESSION["user_id"]);
		$this->tpl->assign("rs",$rs);
		
		$rslist = $this->recommend_m->get_list(3);
		$lpid = $this->trans_lib->safe("lpid");
		if( !empty($lpid) ){
			foreach( $rslist as $key=>$val ){
				if($val["id"]==$lpid){
					$rslist[$key]["checked"] = "selected='selected'";
				}
			}
		}
		$this->tpl->assign("rslist",$rslist);

		$sitetitle = $this->lang["recommend"];
		$this->tpl->assign("sitetitle",$sitetitle);
		$this->tpl->display("recommend.".$this->tpl->ext);
	}

	function save_f()
	{
		if(!$_SESSION["user_id"])
		{
			error('',$this->url('login'));
		}
		$array = array();
		$array["username"] = $this->trans_lib->safe("username");
		$array["cellphone"] = $this->trans_lib->safe("cellphone");
		$array["proname"] = $this->trans_lib->safe("proname");
		$array["appointment_date"] = $this->trans_lib->safe("selorderTime");
		$array["appointment_time"] = $this->trans_lib->safe("selorderTime2");
		$array["remark"] = $this->trans_lib->safe("remark");
		$array["uid"] = $_SESSION["user_id"];
		$array["uname"] = $_SESSION["user_name"];
		$array["postdate"] = $this->system_time;
		$array["status"] = 0;
		if(!$array["username"])
		{
			echo "请输入被推荐客户的姓名";
			exit;
		}
		if(!$array["cellphone"])
		{
			echo "请输入被客户的手机号码";
			exit;
		}
		if(!$array["proname"])
		{
			echo "请选择意向项目";
			exit;
		}
		$checkcellphone = $this->checkcellphone($array["cellphone"],$array["proname"]);
		if($checkcellphone != "0")
		{
			echo "该手机号用户已被推荐过了";
			exit;
		}
		$this->recommend_m->save($array);
		
		/*
		*微信通知
		*/
		$proname = $array["proname"];
		include(LIBS.'/WeixinJs/WeixinSMS.class.php');  
		$WeixinSMS = new \WeixinSMS("wxa9adc2cde1638e75","ac5418ff04ffcc3e0bb05c38bff32821");
		$sql = " select * from  ".$this->db->prefix."list where title='{$proname}'";
		$info = $this->db->get_one($sql);
		$adminid = explode(",",$info['loupanadmin']);
		foreach( $adminid as $key=>$val ){
			$sql = " select * from  ".$this->db->prefix."user where id='{$val}'";
			$uinfo = $this->db->get_one($sql);
			
			$data['first']['value'] = "亲爱的".$uinfo['uname']."，有经纪人提交了新客户！";
			$data['first']['color'] = "#f29700";
			$data['keyword1']['value'] = $array["username"];
			$data['keyword1']['color'] = "#333";
			$data['keyword2']['value'] = $array["cellphone"];
			$data['keyword2']['color'] = "#333";
			$data['keyword3']['value'] = $array["appointment_date"]." ".$array["appointment_time"];
			$data['keyword3']['color'] = "#333";
			$data['keyword4']['value'] = "等待审核";
			$data['keyword4']['color'] = "#333";
			
			$data['remark']['value'] = "快跟客户联系吧！";
			$data['remark']['color'] = "#333";
			$WeixinSMS->doSend($uinfo['openid'], "PozNBsVXYrQhFyhBAN8C04NM59YAWdQ521ZkKXwdGYk",'', $data, $topcolor = '#32749a');//织梦的鱼
		}
			
		echo "1";
		exit;
	}
	
	
	
	//检测手机号是否存在
	function checkcellphone_f()
	{
		$cellphone = $this->trans_lib->safe("cellphone");
		exit($this->checkcellphone($cellphone));
	}
	function checkcellphone($cellphone,$proname)
	{
		if(!$cellphone)
		{
			return "1";
		}
		$rs = $this->recommend_m->customer_from_cellphone2($cellphone,$proname);
		if($rs)
		{
			return "1";
		}
		else
		{
			return "0";
		}
	}
	
	//检测是否自己手机号
	function checkismycellphone_f()
	{
		$cellphone = $this->trans_lib->safe("cellphone");
		exit($this->checkismycellphone($cellphone));
	}
	function checkismycellphone($cellphone)
	{
		if(!$cellphone)
		{
			return "1";
		}
		if($_SESSION["user_id"])
		{
			$rs = $this->user_m->user_my_cellphone($_SESSION["user_id"],$cellphone);
			if($rs)
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
	}
}
?>